/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.validate.min.js";
let chrmIcon = '';

let token = sessionStorage.getItem('supplier_token');

$('.J_imgUpload').on('click', function() {
    let formData = new FormData();
    let file = $('.J_chrmIcon')[0].files[0];
    console.log(file);
    if (file) {

        formData.append('file', file);

        $.ajax({
            url: HOST_ADDR + '/cpmp/file/upload/img',
            type: 'post',
            data: formData,
            processData: false,
            contentType: false,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp) {

                if (resp.success) {
                    chrmIcon = resp.data;
                    alert('上传图片成功');
                    //  location.href = '/supplier_self/product_list.html';
                } else {
                    alert('上传图片失败');
                }
            },
            error: function(err) {
                console.log(err);
            }
        });

    } else {
        alert('请先上传图片');
    }
});

$('.J_add-product-form').validate({
    errorElement: 'label', //default input error message container
    errorClass: 'help-inline', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        iconTitle: {
            required: true
        },
        itdc: {
            required: true
        },
        chrmIcon: {
            required: true
        }
    },

    messages: {
        prodNam: {
            required: "请填写标题"
        },
        prodBrand: {
            required: "请填写介绍"
        },
        chrmIcon: {
            required: "请上传图片"
        }
    },

    invalidHandler: function(event, validator) { //display error alert on form submit
        $('.alert-error', $('.J_add-product-form')).show();
    },

    highlight: function(element) { // hightlight error inputs
        $(element)
            .closest('.control-group').addClass('error'); // set error class to the control group
    },

    success: function(label) {
        label.closest('.control-group').removeClass('error');
        label.remove();
    },

    errorPlacement: function(error, element) {
        error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
    },

    submitHandler: function(form) {
        let $form = $(form);
        let iconTitle = $form.find('.J_iconTitle').val();
        let itdc = $form.find('.J_itdc').val();
        let chrmIconAddr = chrmIcon;

        if (!iconTitle) {
            alert('请填写标题');
        } else if (!itdc) {
            alert('请填写介绍');
        } else if (!chrmIconAddr) {
            alert('请先上传图片');
        } else {

            let data = {
                "iconTitle": iconTitle,
                "itdc": itdc,
                "chrmIcon": chrmIconAddr
            };

            $.ajax({
                url: HOST_ADDR + '/cpmp/splrSlfMng/chrm',
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: "application/json",
                access_token: token,
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp) {
                    if (resp.success) {
                        alert('新增供应商风采成功');
                        // location.href = '/supplier_self/product_list.html';
                    } else {
                        alert(resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });

        }

    }
});

$('.J_cancel').click(function() {
    location.href = '/supplier_self/supplier_chrm.html';
});